//
//  TimeViewController.m
//  LiDaShi_IOS
//
//  Created by mac on 15/8/27.
//  Copyright (c) 2015年 mac. All rights reserved.
//

#import "TimeViewController.h"
#import "ImagePageCell.h"
#import "AFNetworking.h"
#import "orderModel.h"

#import "Singleton.h"


#define ImagePageCellIdentifier @"ImagePageCell"
@interface TimeViewController ()<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>
{
    UICollectionViewFlowLayout *flowLayout;
    NSArray *  cellModel;
    NSArray *  Today_array;
    NSArray *  Tomorrow_array;
    NSArray *  Many_days_array;
    NSArray *  Last_array;
    NSArray *time_arr;
    NSArray * All_array;
    NSDate *senddate;
    
}
@property (nonatomic, strong) UICollectionView *myCollectionView;
@property (nonatomic, strong) NSString *day_date;
@property (nonatomic, strong) NSString *day_dates;

@end

@implementation TimeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    [self setTextTitleViewWithFrame:CGRectMake(0, 0, 60, 20) title:@"查看可预约时间" fontSize:18];
    //    self.tableview.frame=CGRectMake(0, slideControl.bottom, SCREEN_WIDTH, ScreenHeight-64-48-48);
    //
    
    [self addLeftBackBtn:nil];
    //  Today_array = @[@"9.5",@"10",@"10.5",@"11"];

    
    time_arr = @[@"9:00",@"9:30",@"10:00",@"10:30",@"11:00",@"11:30",@"12:00",@"12:30",@"13:00",@"13:30",@"14:00",@"14:30",@"15:00",@"15:30",@"16:00",@"16:30",@"17:00",@"17:30",@"18:00",@"18:30",@"19:00",@"19:30",@"20:00",@"20:30"];
    
    
    
    // 提示
    UILabel *tishi = [[UILabel alloc]init];
    tishi.frame = CGRectMake(0, 27+64, [UIScreen mainScreen].bounds.size.width, 20);
    tishi.text = @"请查看服务时间，灰色表示已被锁定";
    tishi.textAlignment = NSTextAlignmentCenter;
    tishi.textColor = UIColorFromRGB(0xb1b1b1);
    tishi.font = [UIFont systemFontOfSize:15.0];
    [self.view addSubview:tishi];
    
    // 4个按键
    NSArray *date_array = @[@"今天",@"明天",@"后天",[self date]];
    for (int i =0; i<4; i++) {
        UIButton *but = [[UIButton alloc]initWithFrame:CGRectMake(31+i*86, tishi.bottom+10, 55, 55)];
        if (SCREEN_WIDTH<375) {
            but.frame = CGRectMake(25+i*70, tishi.bottom+10, 55, 55);
            
        }
        
        if (i==0) {
            but.backgroundColor = UIColorFromRGB(0x53cac3);
            //给按钮上的文字修改颜色
            [but setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

        }
        else{
            but.backgroundColor = UIColorFromRGB(0xe1e1e1);
             [but setTitleColor:UIColorFromRGB(0x53cac3) forState:UIControlStateNormal];

        }
        
        but.tag =900+i;
        //给按钮上添加文字
        [but setTitle:date_array[i] forState:UIControlStateNormal];
      
        //给按钮设置圆角
        but.layer.cornerRadius =55/2;
        //
        
        but.titleLabel.font = [UIFont systemFontOfSize: 16.0];
        
        
        //给按钮添加点击事假
        [but addTarget:self action:@selector(dian_date:) forControlEvents:UIControlEventTouchUpInside];
        
        if (but.tag == 900) {
            [self dian_date:but];
        }
        [self.view addSubview:but];
    }
    // [self date];
    [self AFN_Request];
    // Do any additional setup after loading the view.
}


-(void)backBtn:(WRBButton *)btn
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

-(void)dian_date:(UIButton *)but{
    
    self.day_date = but.titleLabel.text;
    
    [self.myCollectionView reloadData];
    for (int i=0; i<4; i++) {
        if (but.tag != i+900) {
            UIButton *btn = (UIButton*)[self.view viewWithTag:i+900];
            [btn setTitleColor:UIColorFromRGB(0x53cac3) forState:UIControlStateNormal];
            btn.backgroundColor = UIColorFromRGB(0xe1e1e1);
            
        }else{
            
            [but setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            but.backgroundColor = UIColorFromRGB(0x53cac3);
            if (but.tag -900 ==0) {
                All_array =Today_array;
            }else if(but.tag -900 ==1){
                All_array =Tomorrow_array;
            }else if(but.tag -900 ==2){
                All_array =Many_days_array;
            }else if(but.tag -900 == 3){
                All_array =Last_array;
            }
            [_myCollectionView reloadData];
        }
        
    }
    
    [self print:but.tag-900+1];
    
}
-(void )print:(NSInteger)time {
    time=time-1;
    
    NSDate * date2=[NSDate dateWithTimeIntervalSinceNow:+(time*24*60*60)];
    
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date2];
    NSDate *localeDate = [date2  dateByAddingTimeInterval: interval];
    NSLog(@"当前时间是：%@",localeDate);
    
    
    
    [ self gelinweizhi:localeDate];
    
}
-(void)gelinweizhi:(NSDate *)shijian{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:0];

    long long seconds = [shijian timeIntervalSinceDate:date];
    NSLog(@"%ld",(long)seconds*1000);

    
}

-(void)CollectionView{
    flowLayout= [[UICollectionViewFlowLayout alloc]init];
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 130+64, SCREEN_WIDTH-20, 290) collectionViewLayout:flowLayout];
    if (iPhone5) {
        self.myCollectionView.frame = CGRectMake(10,130+64, SCREEN_WIDTH-20, 350);
    }
    self.myCollectionView.layer.masksToBounds = YES;
    self.myCollectionView.layer.cornerRadius = 4;
    self.myCollectionView.contentInset
    = UIEdgeInsetsMake(0,0,0,0);
    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    [self.myCollectionView registerClass:[ImagePageCell class] forCellWithReuseIdentifier:ImagePageCellIdentifier];
    
    
    flowLayout.scrollDirection =UICollectionViewScrollDirectionVertical;
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    [self.view addSubview:self.myCollectionView];
    flowLayout.minimumLineSpacing = 15;
    
    if (iPhone5) {
        flowLayout.minimumLineSpacing = 10;
    }
}
-(NSString *)date{
    NSDate*date = [NSDate date];
    NSCalendar*calendar = [NSCalendar currentCalendar];
    NSDateComponents*comps;
    
    comps =[calendar components:(NSWeekCalendarUnit | NSWeekdayCalendarUnit |NSWeekdayOrdinalCalendarUnit)
                       fromDate:date];
    // NSInteger week = [comps week]; // 今年的第几周
    NSInteger weekday = [comps weekday]; // 星期几（注意，周日是“1”，周一是“2”。。。。）
    NSLog(@"星期几：%ld",(long)weekday);
    weekday = weekday+3;
    
    if (weekday >7 ) {
        weekday=weekday-7;
    }
    if (weekday == 1) {
        return @"周日";
    } if (weekday ==2) {
        return @"周一";
    } if (weekday == 3) {
        return @"周二";
    } if (weekday == 4) {
        return @"周三";
    } if (weekday == 5) {
        return @"周四";
    } if (weekday == 6) {
        return @"周五";
    } else{
        
        return @"周六";
    }
}
-(void)AFN_Request{
    
    
    
    // 设置已完成个人订单参数
    NSDictionary *dictionary=[NSDictionary dictionaryWithObjectsAndKeys:[self getArtisanId], @"artisanId", nil];
    
    [[WSNetworkApi shareManager]postWithUrl:@"/artisan/getReserveTimeByArtisanId" parameters:dictionary option:RequestCenterCachePolicyNormal sucess:^(id responseObject) {
        
        
        NSLog(@"POST --> %@, %@", responseObject, [NSThread currentThread]); //自动返回主线程
        cellModel  = [responseObject objectForKey:@"timeList"];
        NSLog(@"%@",[responseObject objectForKey:@"message"]);
        Today_array=[cellModel[0] objectForKey:@"time"];
        All_array =Today_array;
        
        
        Tomorrow_array = [cellModel[1] objectForKey:@"time"];
        Many_days_array= [cellModel[2] objectForKey:@"time"];
        Last_array= [cellModel[3] objectForKey:@"time"];
        
        
        [self CollectionView];
    } failur:^(NSError *error) {
        
    }];
    
    
    
    
}


//组数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return time_arr.count;
}
//定义某个cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ImagePageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ImagePageCellIdentifier forIndexPath:indexPath];
    if(indexPath.section==0)
    {
        //  NSLog(@"时间：%@",cellModel[0][indexPath.row]);
        
                cell.label.text = time_arr[indexPath.row];
        cell.layer.cornerRadius = 4;
        NSLog(@"All_array--------%@",All_array);
        if ([All_array containsObject:time_arr[indexPath.row] ]) {
            
            cell.backgroundColor = UIColorFromRGB(0x53cac3);
            cell.label.textColor = [UIColor whiteColor];
            
        }else{
            
            cell.backgroundColor = UIColorFromRGB(0xeaeaea);
            cell.label.textColor = [UIColor blackColor];
        }
        
    }
    else if(indexPath.section==1)
    {
        cell.backgroundColor = [UIColor greenColor];
    }
    return cell;
}
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(68, 31);
}
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(16, 16, 16, 16);
}
//点击某个CEll
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([All_array containsObject:time_arr[indexPath.row] ]) {
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
